/*
 * This is GPL v2 licensed code. See the file LICENSE in the
 * source distribution, or www.gnu.org/copyleft/gpl.html, for
 * more information.
 *
 * Copyright (C) by Håkan Lindqvist 2006, 2007
 */

package PolicyToolGui;


import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;


/** 
 * This is the GUI tool for interacting with the policy algebra
 * packages.
 */
public class PolicyToolGui extends JFrame implements ActionListener {
	/* Command constants */
	private static final String EXITCMD = "EXIT";


	/** 
	 * Constructor.
	 * @param title The title of the window.
	 * @throws GUIException If something goes wrong.
	 */
	public PolicyToolGui(String title) throws GUIException {
		super(title);

		//Make sure we have nice window decorations.
		JFrame.setDefaultLookAndFeelDecorated(true);

		// Window should close on exit.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Make sure that border layout is used.
		setLayout(new BorderLayout());

		Component windowContents = createWindowContents();
		add(windowContents, BorderLayout.CENTER);

		// Add the menu.
		setJMenuBar(createMenuBar());

		//setSize(800, 600);
		pack();
		setResizable(true);
		setVisible(true);
	}


	/** 
	 * Set up the menu. 
	 * 
	 * @return The menubar to ad to the top.
	 * @throws GUIException If something goes wrong.
	 */
	public JMenuBar createMenuBar() throws GUIException {
		JMenuBar menuBar = new JMenuBar();
		JMenu menu;
		JMenuItem menuItem;

		try {
			menu = new JMenu(MenuPropertyLoader.getProperty("menu.file.title"));
			menu.setMnemonic(KeyEvent.VK_F);
			menu.getAccessibleContext().setAccessibleDescription(MenuPropertyLoader.getProperty("menu.file.description"));

			/* Menu entry */
			menuItem = new JMenuItem(MenuPropertyLoader.getProperty("menu.file.entry.exit"));
			menuItem.setMnemonic(KeyEvent.VK_X);
			menuItem.setActionCommand(EXITCMD);
			menuItem.addActionListener(this);
		} catch (IOException e) {
			throw new GUIException("Failed to load menu settings");
		}

		menu.add(menuItem);
		menuBar.add(menu);


		return menuBar;
	}


	/** 
	 * Set up all of the visible components of the user interface.
	 * 
	 * @return The pane that holds the user interface.
	 * @throws GUIException If something goes wrong.
	 */
	private Component createWindowContents() throws GUIException {
		// This panel will hold all of the actual user interface components.
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(4, 4, 4, 4));


		/* Add the left navigation tree */
		panel.add(createPolicyItemsTree(), BorderLayout.WEST);


		// Editing area.
		panel.add(createPolicyEditArea(), BorderLayout.CENTER);


		/* Return the interface setup. */
		return panel;
	}


	/** 
	 * Set up the left policy navigation tree. 
	 * 
	 * @return The navigation tree in a frame ready to be added to a GUI.
	 * @throws GUIException If something goes wrong
	 */
	private Component createPolicyItemsTree() throws GUIException {
		DefaultMutableTreeNode top  = null;
		DefaultMutableTreeNode tree = null;
		DefaultMutableTreeNode node = null;

		try {
			top = new DefaultMutableTreeNode(NavigationPropertyLoader.getProperty("navigation.top"));
		} catch (IOException e) {
			throw new GUIException("Failed to load navigation tree top title");
		}

		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));
		top.add(new DefaultMutableTreeNode("apa"));

		JTree policyItemsTree = new JTree(top);
		JScrollPane navigationScroll = new JScrollPane(policyItemsTree);
		navigationScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		navigationScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

		/* Make the panel to return */
		JPanel panel = new JPanel();
		panel.add(navigationScroll);

		TitledBorder title;
		try {
			title = BorderFactory.createTitledBorder(NavigationPropertyLoader.getProperty("navigation.border"));
		} catch (IOException e) {
			throw new GUIException("Failed to load navigation tree border title");
		}
		panel.setBorder(title);

		return panel;
	}


	/** 
	 * Set up the left policy editing area. 
	 * 
	 * @return The editing area in a frame ready to be added to a GUI.
	 * @throws GUIException If something goes wrong
	 */
	private Component createPolicyEditArea() throws GUIException {

		/* Add this for now - to fill it up with something. */
		JTextArea t = new JTextArea(" Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n Bla Bla Bla \n ");
		JScrollPane editScroll = new JScrollPane(t);

		/* Make the panel to return */
		JPanel panel = new JPanel();
		panel.add(editScroll);

		TitledBorder title;
		try {
			title = BorderFactory.createTitledBorder(EditPropertyLoader.getProperty("edit.border"));
		} catch (IOException e) {
			throw new GUIException("Failed to load edit area border title");
		}
		panel.setBorder(title);

		return panel;
	}


	/** 
	 * Method to implemnt to be an ActionListener.
	 * 
	 * @param e The event that has occured.
	 */
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();

		if (EXITCMD.equals(cmd)) {
			this.dispose(); /* Clean up and close the current window */
			System.exit(0);
		}
	}


	/** 
	 * Starting point.
	 */
	public static void main(String[] args) {
		/* Start up the GUI in a thread safe way */
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					try {
						String title;
						try {
							title = TopPropertyLoader.getProperty("gui.title");
						} catch (IOException e) {
							title = "Policy Tool";
						}
						new PolicyToolGui(title);
					} catch (GUIException e) {
						System.err.println(e.getMessage());
					}
				}});
	}
}
